var Cc = Components.classes;
var Ci = Components.interfaces;
var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch2);
var twitter_login     = prefs.getCharPref("extensions.livetweeter.twitter.login");
var identica_login    = prefs.getCharPref("extensions.livetweeter.identica.login");

var LiveTweeterPrefsPane = {
	openPreferences: function() {
        var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].
                            getService(Ci.nsIWindowMediator);
        var window = windowMediator.getMostRecentWindow("Songbird:Main");

        window.SBOpenPreferences("paneLT");
	},
	
    getPlatform: function() {
		var platform;
			try {
				var sysInfo = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2);
				platform = sysInfo.getProperty("name");
				}
		catch (e) {
			dump("System-info not available, trying the user agent string.\n");
			var user_agent = navigator.userAgent;
			if (user_agent.indexOf("Windows") != -1)
				platform = "Windows_NT";
			else if (user_agent.indexOf("Mac OS X") != -1)
				platform = "Darwin";
			else if (user_agent.indexOf("Linux") != -1)
				platform = "Linux";
			else if (user_agent.indexOf("SunOS") != -1)
				platform = "SunOS";
		}
	return platform; 
	},	
	
	DisableTxtFile: function() {
		var txtfilecheckbox  = document.getElementById("txtfilecheckbox");
			if (txtfilecheckbox.checked == true) {
				document.getElementById("txtfilepath").disabled = true;
				document.getElementById("browsetxtfile").disabled = true;
			}
			if (txtfilecheckbox.checked == false) {
				document.getElementById("txtfilepath").disabled = false;
				document.getElementById("browsetxtfile").disabled = false;
			}
		},  

	LoadDisable: function() {
		var txtfilecheckbox  = document.getElementById("txtfilecheckbox");
		var platform = LiveTweeterPrefsPane.getPlatform();  
			if (txtfilecheckbox.checked == true) {
				document.getElementById("txtfilepath").disabled = false;
				document.getElementById("browsetxtfile").disabled = false;
			}
			if (txtfilecheckbox.checked == false) {
				document.getElementById("txtfilepath").disabled = true;
				document.getElementById("browsetxtfile").disabled = true;
			}
			if (platform != "Linux") {
				document.getElementById("skypepref").setAttribute("disabled", "true");
				document.getElementById("skypeprefcheckbox").disabled = true;
			}
		},  
  
  BrowseTxtFile: function() {
    const nsIFilePicker = Components.interfaces.nsIFilePicker;
    var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
//    fp.init(window, this._strings.getString("ChooseSoundFile"), nsIFilePicker.modeOpen);
    fp.init(window, "text file quick", nsIFilePicker.modeOpen);
//    fp.appendFilter(this._strings.getString("SoundFileFilter") + " (*.txt)", "*.txt");
    fp.appendFilter("text file" + " (*.txt)", "*.txt");

    var ret = fp.show();
    if (ret == nsIFilePicker.returnOK || ret == nsIFilePicker.returnReplace) {
      var file = fp.file;
      var pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch2);
      pref.setCharPref("extensions.livetweeter.txtfile.path", file.path);
      return document.getElementById("txtfile.path").value = file.path;
    }
  },	

	handleLoad: function() {
		
LiveTweeterPrefsPane.LoadDisable();		
		
const Cc = Components.classes;
var getXML = function getXML(url, success, failure) {
  var xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
    .createInstance();
  // run in the background, since we're in xpcomland
  xhr.mozBackgroundRequest = true;
  // force an xml response
  xhr.overrideMimeType('text/xml');
  xhr.onload = function(event) {
    if (xhr.responseXML) {
	  if (xhr.responseText == "No recs :(") {
		  dump("Failed to get XSPF");
		  failure(xhr);
	  } else {
		  success(xhr.responseXML);
	  }
    } else {
      failure(xhr);
    }
  };
  xhr.onerror = function(event) {
    failure(xhr);
  };
  xhr.open('GET', url, true);
  xhr.send(null);
  return xhr;
}
  //var twitter_url  = 'http://twitter.com/users/show.xml?screen_name=' + twitter_login;
  var identica_url = 'http://identi.ca/api/users/show.xml?screen_name=' + identica_login;
  
  /* getXML(twitter_url, function success(xml) {
    function text(tag) {
      var tags = xml.getElementsByTagName(tag);
      if (tags.length) {
        return tags[0].textContent;
      } else {
        return '';
      }
    }
   var twitteravatar = text('profile_image_url');
    self.playcount = parseInt(text('playcount'));
    self.avatar = text('avatar');
    self.profileurl = text('url');

if (twitteravatar == "") {
var twitteravatar = "http://static.twitter.com/images/default_profile_normal.png";
}

var twav  = document.getElementById("twitter-avatar");
var twimg = document.createElement("image");
twimg.setAttribute("src",twitteravatar);
twimg.setAttribute("width", "48");
twimg.setAttribute("height", "48");
twav.appendChild(twimg);
  
  }); */
  	
  getXML(identica_url, function success(xml) {
    function text(tag) {
      var tags = xml.getElementsByTagName(tag);
      if (tags.length) {
        return tags[0].textContent;
      } else {
        return '';
      }
    }
   var identicaavatar = text('profile_image_url');
    self.playcount = parseInt(text('playcount'));
    self.avatar = text('avatar');
    self.profileurl = text('url');

if (identicaavatar == "") {
var identicaavatar = "http://theme.status.net/0.8.0/identica/default-avatar-stream.png";
}

var idav  = document.getElementById("identica-avatar");
var idimg = document.createElement("image");
idimg.setAttribute("src",identicaavatar);
idimg.setAttribute("width", "48");
idimg.setAttribute("height", "48");
idav.appendChild(idimg);
  
  });
	}
}


